//
//  UpdateAlertView.m
//  BaishidaiUser
//
//  Created by mac book on 2018/12/10.
//  Copyright © 2018年 ly. All rights reserved.
//

#import "UpdateAlertView.h"


#define MM_MAINCOLOR [UIColor colorWithRed:8.0/255.0 green:159.0/255.0 blue:251.0/255.0 alpha:1.0]
#define MM_FONT333 [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0]
#define MM_FONT999 [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0]

@implementation UpdateAlertView


-(instancetype)initWithTitle:(NSString *)title detailTitle:(NSString *)detail image:(UIImage *)images btnTitle:(NSString *)btnTitle detailHeight:(CGFloat)floa isShowClose:(BOOL)isShow{
    self =[super init];
    if (self) {
        self.images = [[UIImage alloc] init];
        self.images = images;
        self.title = title;
        self.detailTitle = detail;
        self.btnTitle = btnTitle;
        self.floa = floa;
        self.isShow = isShow;
        [self initUI];
        
    }
    return self;
}
-(void)initUI{
    CGFloat screenW =[UIScreen mainScreen].bounds.size.width;
    CGFloat screenH =[UIScreen mainScreen].bounds.size.height;
    CGFloat alertW =screenW *0.6;
    CGFloat alertH =alertW/3*4;
    
    [self setFrame:CGRectMake(0, 0, screenW, screenH)];
    [self setBackgroundColor:[UIColor clearColor]];
    
    UIView *shandow =[[UIView alloc]initWithFrame:self.frame];
    [shandow setBackgroundColor:[UIColor lightGrayColor]];
    //    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeShandow)];
    //    [shandow addGestureRecognizer:tap];
    [shandow setAlpha:0.5];
    [self addSubview:shandow];
    
    UIView *alert =[[UIView alloc]init];
    [alert setBackgroundColor:[UIColor whiteColor]];
    alert.layer.cornerRadius =5;
    alert.layer.masksToBounds =YES;
    [self addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(alertW, alertH));
        //        make.center.equalTo(self.center);
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    
    UIButton *action =[[UIButton alloc]init];
    action.layer.cornerRadius =5;
    [action.titleLabel setFont:[UIFont systemFontOfSize:16]];
    action.layer.masksToBounds =YES;
    [action setBackgroundColor:MM_MAINCOLOR];
    [action setTitle:self.btnTitle forState:UIControlStateNormal];
    [action addTarget:self action:@selector(nextActionEvent) forControlEvents:UIControlEventTouchUpInside];
    [alert addSubview:action];
    [action mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(alert.mas_bottom).with.offset(-20);
        make.left.equalTo(alert.mas_left).with.offset(40);
        make.right.equalTo(alert.mas_right).with.offset(-40);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *detailLab =[[UILabel alloc]init];
    [detailLab setNumberOfLines:4];
    [detailLab setFont:[UIFont systemFontOfSize:14]];
    [detailLab setTextColor:MM_FONT999];
    [detailLab setTextAlignment:NSTextAlignmentCenter];
    [detailLab setText:self.detailTitle];
    [alert addSubview:detailLab];
    [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(action.mas_top).with.offset(-10);
        make.left.equalTo(action.mas_left);
        make.height.mas_equalTo(self.floa);
        make.width.equalTo(action.mas_width);
    }];
    
    UILabel *title =[[UILabel alloc]init];
    [title setFont:[UIFont boldSystemFontOfSize:16]];
    [title setTextColor:MM_FONT333];
    [title setTextAlignment:NSTextAlignmentCenter];
    [title setText:self.title];
    [alert addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(detailLab.mas_top).with.offset(-10);
        make.left.equalTo(detailLab.mas_left);
        make.height.mas_equalTo(25);
        make.width.equalTo(detailLab.mas_width);
    }];
    
    UIImageView *imageV =[[UIImageView alloc]init];
    //    imageV.backgroundColor = [UIColor yellowColor];
    [imageV setImage:self.images];
    [imageV setContentMode:UIViewContentModeScaleAspectFit];
    [alert addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(alert.mas_width);
        make.bottom.equalTo(title.mas_top).with.offset(0);
        make.left.equalTo(0);
        make.top.equalTo(alert.mas_top);
    }];
    

    
    UIButton *off_btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [off_btn setImage:[UIImage imageNamed:@"icon_lzh_off"] forState:(UIControlStateNormal)];
    [alert addSubview:off_btn];
    [off_btn addTarget:self action:@selector(closeShandow) forControlEvents:(UIControlEventTouchUpInside)];
    [off_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(alert);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    if (self.isShow) {
        off_btn.hidden = NO;
    }else{
        off_btn.hidden = YES;
    }
    
}


-(void)nextActionEvent{
    self.nextAction();
}


-(void)closeShandow{
    [self removeFromSuperview];
}

-(void)nextActionEv:(Action)action{
    self.nextAction =action;
}



-(instancetype)initWithLoginAlert{
    
    self =[super init];
    if (self) {
    
        [self initLoginALertUI];
        
    }
    return self;
    
}

-(void)initLoginALertUI{
    
    CGFloat screenW =[UIScreen mainScreen].bounds.size.width;
    CGFloat screenH =[UIScreen mainScreen].bounds.size.height;
    CGFloat alertW =screenW *0.7;
    CGFloat alertH =alertW/4*5;
    
    [self setFrame:CGRectMake(0, 0, screenW, screenH)];
    [self setBackgroundColor:[UIColor clearColor]];
    
    UIView *shandow =[[UIView alloc]initWithFrame:self.frame];
    shandow.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];

    //    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeShandow)];
    //    [shandow addGestureRecognizer:tap];
    [self addSubview:shandow];
    
    UIImageView *alert =[[UIImageView alloc]init];
    alert.image = [UIImage imageNamed:@"my_loginalert"];
    alert.contentMode = UIViewContentModeScaleAspectFit;
    alert.layer.masksToBounds = YES;
    alert.userInteractionEnabled = YES;
    [self addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(alertW, alertH));
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    
    UIButton *action =[[UIButton alloc]init];
    action.layer.cornerRadius =18;
    [action.titleLabel setFont:[UIFont systemFontOfSize:14]];
    action.layer.masksToBounds =YES;
    [action setBackgroundColor:[UIColor lightGrayColor]];
    [action setTitle:@"随便看看" forState:UIControlStateNormal];
    [action addTarget:self action:@selector(loginActionEvent:) forControlEvents:UIControlEventTouchUpInside];
    [alert addSubview:action];
    [action mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(alert.mas_bottom).with.offset(-40);
        make.left.equalTo(alert.mas_left).with.offset(40);
        make.right.equalTo(alert.mas_right).with.offset(-40);
        make.height.mas_equalTo(36);
    }];
    
    
    
    UIButton *action1 =[[UIButton alloc]init];
    action1.layer.cornerRadius =18;
    [action1.titleLabel setFont:[UIFont systemFontOfSize:14]];
    action1.layer.masksToBounds =YES;
    [action1 setBackgroundColor:MM_MAINCOLOR];
    [action1 setTitle:@"去登录" forState:UIControlStateNormal];
    [action1 addTarget:self action:@selector(loginActionEvent:) forControlEvents:UIControlEventTouchUpInside];
    [alert addSubview:action1];
    [action1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(action.mas_top).with.offset(-10);
        make.left.equalTo(action.mas_left);
        make.height.width.equalTo(action);
    }];
    
    action.tag = 10001;
    action1.tag = 10002;

}

-(void)loginActionEvent:(UIButton *)button{
    
    if (self.loginButtonViewBlock) {
        self.loginButtonViewBlock(button.tag);
        [self removeFromSuperview];

    }

}

#pragma mark - 弹出 -
- (void)showAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];

}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
